print("");

# 1.字符串定义
## 字面量
"lilei"

## 字符串定义
str = "lilei"

## 字符串中的数不可增删改
# str[0] = 'x'

# 2.字符串索引
print("----- 字符串索引 -----\n");
str_demo = "my name is lilei"
print('第[%2d]个元素为:%s' %(1, str_demo[0]));
print('第[%2d]个元素为:%s' %(2, str_demo[1]));
print('第[%2d]个元素为:%s' %(3, str_demo[2]));
print('第[%2d]个元素为:%s' %(-1, str_demo[-1]));
print('第[%2d]个元素为:%s' %(-2, str_demo[-2]));
print('第[%2d]个元素为:%s' %(-3, str_demo[-3]));

# 3.字符串操作
## index()--查找元素下标(多个时返回最前面的)
print("元素[%s]的下标为:%d" %('i', str_demo.index('i')));
print("元素[%s]的下标为:%d" %('name', str_demo.index('name')));

## replace()--替换
new_str = str_demo.replace("am", "xx")
print("替换前的字符串为:%s\n替换后的字符串为:%s" %(str_demo, new_str));

## split()--分割
str_list = str_demo.split(" ")
print(str_list)

## strip()--去掉首尾字符串
str_demo_2 = " Han Meimei "
print(str_demo_2.strip()) # 去掉收尾空格
print("|%s|" %(str_demo_2.strip())) # 去掉首尾空格
str_demo_3 = "ssHan Meimeiss"
print("|%s|" %(str_demo_3.strip("ss"))) # 去掉首尾指定字符串

## count()--统计字符出现个数
print("[%s]字符串中[%s]出现了[%d]次" %(str_demo, "i", str_demo.count("i")))

## len()--统计字符长度
print("[%s]字符串长度为:%d" %(str_demo, len(str_demo)))

# 4.元组遍历
print("----- 字符串遍历1 -----");
index = 0;
while index < len(str_demo):
    print(str_demo[index], end = ",");
    index += 1;
print("")

print("----- 字符串遍历2 -----");
for item in str_demo:
    print(item, end = ",");
print("")